<!-- TODO: 应该和Pagination组件合并 -->
<!-- 分页组件 -->
<template>
    <div class="pagination-container">
        <el-pagination
            class="f-r"
            background
            :current-page="currentPage"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            :layout="layout"
            :total="total"
            :pager-count="pagerCount"
            @size-change="pageSizeChange"
            @current-change="pageCurrentChange"
        >
            <span>
                <slot />
            </span>
        </el-pagination>
    </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
    name: 'Pagination',
    props: {
        total: {
            required: true,
            type: Number,
            default: 0
        },
        page: {
            type: Number,
            default: 1
        },
        limit: {
            type: Number,
            default: 20
        },
        pageSizes: {
            type: Array,
            default() {
                return [5, 10, 20, 50, 100]
            }
        },
        layout: {
            type: String,
            default: 'jumper,prev, pager, next,sizes,total'
        },
        background: {
            type: Boolean,
            default: true
        },
        autoScroll: {
            type: Boolean,
            default: true
        },
        pagerCount: {
            type: Number,
            default: 7
        }
    },
    computed: {
        currentPage: {
            get() {
                return this.page
            },
            set(val) {
                // this.$emit('update:page', val)
            }
        },
        pageSize: {
            get() {
                return this.limit
            },
            set(val) {
                // this.$emit('update:limit', val)
            }
        }
    },
    methods: {
        // 一页数据
        pageSizeChange(val) {
            this.$emit('pagination', { page: this.currentPage, limit: val })
            if (this.autoScroll) {
                scrollTo(0, 800)
            }
        },
        // 页数跳转
        pageCurrentChange(val) {
            this.$emit('pagination', { page: val, limit: this.pageSize })
            if (this.autoScroll) {
                scrollTo(0, 800)
            }
        }
    }
}
</script>
